﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseAccess;
using System.Data;

namespace Liguo.Administration
{
    public partial class AdministrationInformation : System.Web.UI.Page
    {
        SqlIt sql = new SqlIt();
        //DataSet类承接查询结果
        DataSet ds = new DataSet();

        protected void Page_Load(object sender, EventArgs e)
        {
            ds = sql.SelectAll("AdminInfo");
            if (!IsPostBack)
            {
                bind(ds);
            }
        }

        protected void search_Click(object sender, EventArgs e)
        {
            string sqlstr = "";
            string search_input=InputAdmin.Text;
            if (!search_input.Equals(""))
            { 
                sqlstr += "AdminName='" + search_input+ "'";
            }
            if (sqlstr == "")
            {
                ds = sql.SelectAll("AdminInfo");
            }
            else
            {
                ds = sql.SpecialItem("AdminInfo", sqlstr);
            }
            bind(ds);
        }
        //绑定          
        public void bind(DataSet ds)
        {
            GridView1.DataSource = ds;
            GridView1.DataKeyNames = new string[] { "ID" };//主键
            GridView1.DataBind();
            DropDownList ddl;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                DataTable dt = new DataTable();
                DataRow drv = dt.NewRow();
                drv = ds.Tables[0].Rows[i];
                if (Convert.ToBoolean(drv["Status"]) == false)
                {
                    ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
                    //ddl.SelectedIndex = 0;
                    ddl.Items.FindByValue("0").Selected = true;
                }
                if (Convert.ToBoolean(drv["Status"]) == true)
                {
                    ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
                    //ddl.SelectedIndex = 1;
                    ddl.Items.FindByValue("1").Selected = true;
                }
            }

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Boolean judge=false;
            // Response.Write("<script>confirm('确认要改变？')</script>");
            DropDownList DropDownList1 = sender as DropDownList;
            int index = (DropDownList1.NamingContainer as GridViewRow).RowIndex;//命名容器
            string id = GridView1.DataKeys[index].Value.ToString();//设定的datakey属性
            string selectedv = DropDownList1.SelectedValue;
            if (selectedv.Equals("0"))
            {
                //Response.Write("<script>confirm('确认要禁用？')</script>");
                ds.Tables[0].Rows[index]["Status"] = selectedv;
                Dictionary<string, string> ud = new Dictionary<string, string>();
                ud["Status"] = "0";
                string constriant;
                constriant = "ID='" + id + "'";
                sql.UpdateMess("AdminInfo", ud, constriant);
                Response.Write("<script>alert('该管理员已禁用！')</script>");
            }
            else if (selectedv.Equals("1"))
            {
                // Response.Write("<script>confirm('确认要启用？')</script>");
                ds.Tables[0].Rows[index]["Status"] = selectedv;
                Dictionary<string, string> ud = new Dictionary<string, string>();
                ud["Status"] = "1";
                string constriant;
                constriant = "ID='" + id + "'";
                sql.UpdateMess("AdminInfo", ud, constriant);
                Response.Write("<script>alert('该管理员已启用！')</script>");
            }
            bind(ds);
        }
    }
}